//package com.huawei.codearts.tools.tool;
//
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.List;
//import java.util.stream.Collectors;
//
//import jakarta.servlet.*;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//@Component
//public class IpFilter implements Filter {
//
//    @Value("${ip.whitelist}")
//    private String whitelistStr;
//
//    @Value("${ip.blacklist}")
//    private String blacklistStr;
//
//    private List<String> whitelist;
//    private List<String> blacklist;
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        whitelist = Arrays.stream(whitelistStr.split(","))
//                .map(String::trim)
//                .collect(Collectors.toList());
//        blacklist = Arrays.stream(blacklistStr.split(","))
//                .map(String::trim)
//                .collect(Collectors.toList());
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        HttpServletRequest httpRequest = (HttpServletRequest) request;
//        String ip = httpRequest.getRemoteAddr();
//
//        if (whitelist.contains(ip)) {
//            // 白名单IP，直接通过
//            chain.doFilter(request, response);
//        } else if (blacklist.contains(ip)) {
//            // 黑名单IP，拒绝访问
//            HttpServletResponse httpResponse = (HttpServletResponse) response;
//            httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "IP is in blacklist");
//        } else {
//            // 非黑白名单IP，根据业务需求处理（例如：允许或拒绝）
//            chain.doFilter(request, response);
//        }
//    }
//
//    @Override
//    public void destroy() {
//        // 清理资源，如果有的话
//    }
//
//}
//
